/*
 * ToolBarHandler class is coded to handle dynamic toolbar
 * configuration and generation operations with methods which
 * read xml config files and creates objects in runtime
 * THIS CLASS IS FOR v1.1
 */
package com.meraklisina.latexeditor ;

import java.util.ArrayList ;
import java.util.Collections ;
import javax.swing.JPanel ;
import javax.swing.JToolBar ;
import org.w3c.dom.* ;

/**
 *
 * @author kursattopcu
 */
public class ToolBarHandler
{
    private static ToolBarHandler handler = null ;

    private AppConfigReader appConf ;

    public JPanel container = null ;

    private ArrayList<LatexToolBar> toolbars = null ;

    public static ToolBarHandler getHandler( )
    {
        if( handler == null )
        {
            handler = new ToolBarHandler( ) ;
        }
        return handler ;
    }

    /*
     * ToolBarHandler constructor.
     * */
    private ToolBarHandler( )
    {
        //this.container = containerFrame;
    }

    public void setContainer( JPanel container )
    {
        this.container = container ;
    }

    /*
     * Toolbar generator method - reads config from xml file 
     * */
    public JPanel Generate( String configFilePath ) throws Exception
    {
        try
        {
            appConf = new AppConfigReader( configFilePath ) ;

            toolbars = appConf.GetToolBarList( ) ;

            for( int i = 0; i < getToolbars( ).size( ); i++ )
            {
                LatexToolBar tb = getToolbars( ).get( i ) ;
                ToolBarConfigReader toolbarConf = new ToolBarConfigReader( tb.getConfigFilePath( ) ) ;
                tb = toolbarConf.GenerateButtons( tb ) ;
                //tb.setVisible( true ) ; //????
                //AddToolBarToPanel( tb ) ;
                if( tb.isAvailable( ) )
                {
                    AddToolBarToPanel( tb ) ;
                }
                toolbars.set( i, tb ) ;
            }
            return this.container ;
        }
        catch( Exception ex )
        {
            String errorString = ExceptionHandler.ProcessException( ex ) ;
            throw new Exception( errorString ) ;
            //            return null;
        }

    }

    public JPanel AddToolBarToPanel( LatexToolBar toolbar )
    {
        return AddToolBarToPanel( toolbar, true ) ;
    }

    public JPanel AddToolBarToPanel( LatexToolBar toolbar, boolean refreshContainer )
    {
        this.container.add( toolbar ) ;
        toolbar.setVisibleOnScreen( true ) ;
        if( refreshContainer )
            this.container.revalidate( ) ;
        return this.container ;
    }

    public JPanel RemoveToolBarFromPanel( LatexToolBar toolbar )
    {
        return RemoveToolBarFromPanel( toolbar, true ) ;
    }

    public JPanel RemoveToolBarFromPanel( LatexToolBar toolbar, boolean refreshContainer )
    {
        this.container.remove( toolbar ) ;
        toolbar.setVisibleOnScreen( false ) ;
        if( refreshContainer )
            this.container.revalidate( ) ;
        return this.container ;
    }

    //    public JToolBar CreateToolbar(String toolBarName, int width, int height) {
    //        JToolBar toolbar = new JToolBar(toolBarName, JToolBar.HORIZONTAL);
    //        toolbar.setVisible(true);
    //        toolbar.setSize(width, height);
    //        return toolbar;
    //    }

    /**
     * @return the toolbars
     */
    public ArrayList<LatexToolBar> getToolbars( )
    {
        return toolbars ;
    }

    public static void sort( ArrayList<LatexToolBar> list )
    {
        Collections.sort( list ) ;
        // reset orders
        for( int i = 0; i < list.size( ); i++ )
        {
            LatexToolBar ltb = list.get( i ) ;
            ltb.setOrder( i + 1 ) ;
        }
    }

    public void refreshVisuals( )
    {
        this.container.removeAll( ) ;
        for( LatexToolBar tb : this.toolbars )
        {
            if( tb.isAvailable( ) )
            {
                AddToolBarToPanel( tb, false ) ;
            }
        }
        this.container.revalidate( ) ;
    }

    public void serializeToolBars( )
    {
        Document domDoc = XmlHandler.createXmlDocument( ) ;
        Element toolbarsElement = domDoc.createElement( "ToolBars" ) ;

        for( LatexToolBar ltb : this.toolbars )
        {
            Element toolbarElement = domDoc.createElement( "ToolBar" ) ;
            toolbarElement.setAttribute( "name", ltb.getName( ) ) ;
            toolbarElement.setAttribute( "configFilePath", ltb.getConfigFilePath( ) ) ;
            if( ltb.isAvailable( ) )
            {
                toolbarElement.setAttribute( "visible", "Y" ) ;
            }
            if( ltb.getOrder( ) != -1 )
            {
                toolbarElement.setAttribute( "order", "" + ltb.getOrder( ) ) ;
            }
            toolbarsElement.appendChild( toolbarElement ) ;
        }
        domDoc.appendChild( toolbarsElement ) ;
        String xmlString = XmlHandler.createXmlString( domDoc ) ;

        this.appConf.writeToolbarConfigFile( xmlString ) ;
        //System.out.println(xmlString);
    }
    /*
    public JToolBar AddNewButton(JToolBar toolbar, String buttonName, String iconPath,
    String toolTip, String prefix, String suffix, String expressionTemplate)
    {
    LatexButton button = new LatexButton(buttonName, iconPath, toolTip,
    prefix, suffix, expressionTemplate);

    toolbar.add(button);
    return toolbar;
    }
     * */
}
